define(function (require, exports, module) {
    /**
     * 行按钮控件
     */
    var $ = require("jquery");
    var angular = require('angular');
    var expanderModule = angular.module('rowToolbar', []);
    expanderModule.directive('rowToolbar', ['$parse', function ($parse) {
        return {
            restrict: 'EA',
            replace: false,
            transclude: false,
            scope: {
                title: '@',
                textField: '@',
                buttons: "=",
                minButtonsShow: '@',
                onclick: "&"
            },
            link: function ($scope, element, attrs) {
                $scope.$applyAsync();
                setTimeout(function(){
                    var offset = parseInt($scope.minButtonsShow) - 1;
                    var btns = offset >= 0 ? $(element).children(':gt(' + offset + ')') : $(element).children();
                    if (btns.length == 0) {
                        return;
                    }
                    $(element).append('<span class="row-toolbar-head glyphicon glyphicon-option-horizontal c-gray"></span>')
                        .append('<div class="top-userinfo-menu top-row-toolbar-menu" style="display: none;">' +
                        '<div class="top-userinfo-menu-list top-row-toolbar-menu-list">' +
                        '</div>' +
                        '<i class="top-userinfo-menu-arrow"></i>' +
                        '</div>');
                    $(element).find('.top-row-toolbar-menu-list').append(btns);
                    $(element).on('click', '.row-toolbar-head', function () {
                        $('.top-row-toolbar-menu').hide();
                        $(this).next().toggle();
                    })
                    $(document).on('click', function (e) {
                        if ($(e.target).is('.row-toolbar-head')) {
                            return;
                        }
                        $('.top-row-toolbar-menu').hide();
                    })
                },0);


                $scope.click = function (item) {
                    setTimeout(function () {
                        var call = $parse($scope.onclick)
                        $scope.$apply(function () {
                            call() && call()(item);
                        });

                    }, 0);
                };
            }
        }
    }]);

});